package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Eric
 * @version v1.0
 * @package com.leyou.item.service
 * @date 2019/6/26 15:02
 * @description:
 **/
@Service
public class SpecService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    /**
     * 规格组的查询
     * @param cid
     * @return
     */
    public List<SpecGroupDTO> querySpecGroupByCid(Long cid) {
        //根据categoryId查询规格组的数据
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        List<SpecGroup> list = specGroupMapper.select(specGroup);
        if (CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list,SpecGroupDTO.class);
    }


    /**
     * 根据gid查询规格参数数据
     * @param gid
     * @return
     */
    public List<SpecParamDTO> querySpecParamByGid(Long gid,Long cid,Boolean searching) {

        if (gid==null && cid==null ){
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }

        //查询规格参数
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        List<SpecParam> list = specParamMapper.select(specParam);
        if (CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list,SpecParamDTO.class);
    }

    /**
     * 新增规格组
     * @param dto
     * @return
     */
    public void saveSpecGroup(SpecGroupDTO dto) {
        SpecGroup specGroup = BeanHelper.copyProperties(dto, SpecGroup.class);
        int count = specGroupMapper.insertSelective(specGroup);
        if (count != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 修改规格组
     * @param dto
     * @return
     */
    public void updateSpecGroup(SpecGroupDTO dto) {
        SpecGroup specGroup = BeanHelper.copyProperties(dto, SpecGroup.class);
        int count = specGroupMapper.updateByPrimaryKeySelective(specGroup);
        if (count!=1){
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    /**
     * 根据分类Id查询规格组和规格参数
     * @param cid
     * @return sku的集合
     */
    public List<SpecGroupDTO> querySpecs(Long cid) {

        List<SpecGroupDTO> specGroupDTOS = querySpecGroupByCid(cid);

        //利用cid查询该类下所有的规格参数，并以组id进行分组，map的key是组id，value是该组id下的规格参数的集合
        Map<Long, List<SpecParamDTO>> map = querySpecParamByGid(null, cid, null).stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));

        for (SpecGroupDTO specGroupDTO : specGroupDTOS) {
            //利用组id与map集合的key相等，取value值赋值给规格组的属性params
            specGroupDTO.setParams(map.get(specGroupDTO.getId()));
        }
        return specGroupDTOS;
    }
}
